from django.shortcuts import render_to_response
from django.http import Http404, HttpResponseRedirect
from django.template import RequestContext
from django.views.decorators.csrf import csrf_exempt
from application.intelligence.models import Article
import os, datetime,commands,hashlib,random

# Create your views here.


def gallery_list(request):
    user = request.user
    return render_to_response('gallery.html', locals())

def calendar_list(request):
    user = request.user
    return render_to_response('calendar.html', locals())

def forms_list(request):
    user = request.user
    return render_to_response('forms.html', locals())

def my_blog(request):
    user = request.user
    list = Article.objects.filter(author=user)
    for article in list:
        return render_to_response('blog/my_blog.html', locals())

def blog_list(request):
    user = request.user
    list = Article.objects.all()
    for article in list:
        return render_to_response('blog/blog_list.html', locals())

@csrf_exempt
def blog_up(request):
    user = request.user
    if request.method == 'POST':
        try:
            data = Article(title=request.POST['title'], content=request.POST['content'], author=user, time=datetime.datetime.now())
            data.save()
            return render_to_response('notice/blog_up_success.html', context_instance=RequestContext(request))
        except:
            return render_to_response('notice/blog_up_error.html', context_instance=RequestContext(request))
    else:
        return render_to_response('blog/blog_up.html', locals(), context_instance=RequestContext(request))

@csrf_exempt
def get_mac(request):
    user = request.user
    ntcs = commands.getoutput('ntc')
    ntcs = ntcs.split(',')
    if request.method== 'POST':
        ip = request.POST['ip']
        ntc = request.POST['ntc']
        try:
            mac = commands.getoutput("mac %s %s"%(ntc, ip))
            return render_to_response('get_mac.html', locals(), context_instance=RequestContext(request))
        except:
            return 'error'
    else:
        return render_to_response('get_mac.html', locals(), context_instance=RequestContext(request))
